package top.letscode.adclicker.ad;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import top.letscode.adclicker.annotation.Union;
import top.letscode.adclicker.driver.WebDriverHelper;
import top.letscode.adclicker.util.Constants;
import top.letscode.adclicker.util.RandomUtil;

@Union(name = "sophiaa5", desc = "A5联盟http://lm.admin5.com", type = Constants.UNION_TYPE_MOBILE)
public class SophiaA5Union extends AbstractUnion {

  private int clickTimes = 0;

  @Override
  public void click(WebDriver webDriver) {
    try {
      boolean isAd1 = clickTimes % 2 == 0;
      boolean isAd2 = clickTimes % 2 == 1;

      WebElement iframe = null;
      if (isAd1) {
        System.out.println("start click ad1...");
        iframe = WebDriverHelper.findElement(By.xpath("//*[@id=\"ad1Div\"]//iframe[1]"));
      } else if (isAd2) {
        System.out.println("start click ad2...");
        iframe = WebDriverHelper.findElement(By.xpath("//*[@id=\"ad2Div\"]//iframe[1]"));
      }
      int width = RandomUtil.randomInt(iframe.getSize().getWidth());
      int height = RandomUtil.randomInt(iframe.getSize().getHeight());

      Actions actions = new Actions(webDriver);
      // 随机一个坐标点击
      actions.moveToElement(iframe, width, height).click().perform();
    } finally {
      clickTimes++;
    }
  }

  @Override
  public float getRate() {
    // 单价高，适当控制。单次1.5毛
    return ((1.0f * RandomUtil.randomInt(3)) / 100) + 0.07f;
  }
}
